//
//  ECCalenderCell.swift
//  summaryGraph
//
//  Created by jieliapp on 2017/2/23.
//  Copyright © 2017年 飞天江郎. All rights reserved.
//

import UIKit

class ECCalenderCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

    
    
    var cCollectionView:UICollectionView!
    var cTitleLab:UILabel!
    
    var selfViewSW:CGFloat!
    var selfViewSH:CGFloat!
    
    var cellDate:Date!
    var cellArrayData:Array<String>!
    var cellNowDays:Date!
    
    
    var tagToday:Bool!
    var todayStr:String!
    
    init(style: UITableViewCellStyle, reuseIdentifier: String?,createDate:Date,Cellsize:CGSize) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selfViewSW = Cellsize.width
        selfViewSH = Cellsize.height
        cellDate = createDate
        
        cellArrayData = CalenderToos.monthDayArrayFromDate(date: createDate)
        
        titleFrameSelecation(date: createDate)
        tagToday = false
        
        cellNowDays = NSDate() as Date
        
        self.yearAndMonthCompare()
        
        let flowLayout = UICollectionViewFlowLayout.init()
        
        cCollectionView = UICollectionView.init(frame: CGRect(x:0,y:cTitleLab.frame.size.height,width:selfViewSW,height:selfViewSH - cTitleLab.frame.size.height), collectionViewLayout: flowLayout)
        
        cCollectionView.backgroundColor = UIColor.clear
        cCollectionView.isScrollEnabled = true
        cCollectionView.dataSource = self
        cCollectionView.delegate = self
        cCollectionView.register(UINib(nibName:"ECCalenderDayCell",bundle:nil), forCellWithReuseIdentifier: "dayCell")
        
        self.addSubview(cCollectionView)
        
        
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        
         super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
        
    }
    
    
    
    func yearAndMonthCompare() {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM"
        formatter.timeZone = NSTimeZone.init(forSecondsFromGMT: 8*3600) as TimeZone!
        let date1 = formatter.string(from: cellDate)
        let date2 = formatter.string(from: cellNowDays)
        
        
        
        if date1 == date2{
        
            tagToday = true
            let matter = DateFormatter()
            matter.dateFormat = "yyyy-MM-dd"
            matter.timeZone = NSTimeZone.init(forSecondsFromGMT: 8*3600) as TimeZone!
            todayStr = (matter.string(from: cellNowDays)).components(separatedBy: "-").last
        }
        
    }
    
    
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize.init(width: selfViewSW/7, height: selfViewSW/7)
        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dayCell", for: indexPath) as! ECCalenderDayCell
        
        let items:String = cellArrayData[indexPath.row]
        
        if items == "kong" {
            cell.labelBtn.setTitle("", for: UIControlState.normal)
        }else{
            
            cell.labelBtn.setTitle(items, for: UIControlState.normal)
            if true == tagToday {
                if items == todayStr {
                    cell.backgroundColor = UIColor.orange
                    
                }
            }
        }
        
        return cell
        
        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cellArrayData.count
    }
    
    
    
    
    
    
    
    /// 绘制月份头部标志
    ///
    /// - Parameter date: 日期
    func titleFrameSelecation(date:Date){
        
        let index = CalenderToos.calculationWeekDayWithDate(fromDate: date)
    
        let slf = CGFloat(index-1)*(selfViewSW/7)
        
        cTitleLab = UILabel.init(frame:CGRect(x:slf,y:0,width:selfViewSW/7,height:selfViewSW/7))
        cTitleLab.textColor = UIColor.orange
        cTitleLab.textAlignment = .center
        self.addSubview(cTitleLab)
        
        let lineView = UIImageView.init(frame: CGRect(x:slf,
                                                      y:selfViewSW/7-1,
                                                      width:selfViewSW - slf+selfViewSW/7,
                                                      height:0.5))
        lineView.backgroundColor = UIColor.lightGray
        self.addSubview(lineView)
        
        let formate = DateFormatter()
        formate.dateFormat = "MM"
        let monthStr:String = formate.string(from: date) + "月"
        cTitleLab.text = monthStr
        
    }
    
    
   
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }

}
